import { parse } from 'url';
import { TableListItem, TableListParams } from './data';

function getPermissionList(
  req: { url: any },
  res: {
    json: (
      arg0: {
        body: {
          list: TableListItem[];
          count: number,
        }
      },
    ) => void;
  },
  u: any,
) {
  let url = u;
  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    // eslint-disable-next-line prefer-destructuring
    url = req.url;
  }

  const params = (parse(url, true).query as unknown) as TableListParams;

  let dataSource: TableListItem[] = [];

  for (let i = (params.currentPage - 1) * 10; i < 10 * params.currentPage; i++) {
    dataSource.push({
      key: i,
      id: i + 1,
      can_withdraw: 1,
      name: `孙悟空${i + 1}`,
      withdraw_oper_user_name: 'xiaoqiang',
      withdraw_oper_time: '2019-06-26 17:33:25',
    });
  }

  if (params.name) {
    dataSource = dataSource.filter(data => data.name.indexOf(params.name) > -1);
  }

  const result = {
    code: 0,
    body: {
      list: dataSource,
      count: 30,
    },
  };

  return res.json(result);
}

export default {
  'GET /withdraw_perm/index': getPermissionList,
};
